import plugin from '../../../lib/plugins/plugin.js'
import lodash from 'lodash'
import { Common, Data } from '../components/index.js'
import Theme from './help/theme.js'

export class fyhelp extends plugin {
  constructor() {
    super({
      name: '修仙_帮助',
      dsc: '',
      event: 'message',
      priority: 2000,
      rule: [
        {
          reg: '^#?修仙(插件)?帮助$',
          fnc: 'message'
        }
      ]
    });
  }

  async message() {
    return await help(this.e);
  }
}

async function help(e) {
  const { diyCfg, sysCfg } = await Data.importCfg('help')

  const helpConfig = lodash.defaults(diyCfg.helpCfg || {}, sysCfg.helpCfg || {})
  const helpList = diyCfg.helpList || sysCfg.helpList
  const helpGroup = []

  helpList.forEach(group => {
    if (group.auth === 'master' && !e.isMaster) return

    group.list.forEach(helpItem => {
      const icon = parseInt(helpItem.icon)
      helpItem.css = icon ? `background-position:-${(icon - 1) % 10 * 50}px -${Math.floor((icon - 1) / 10) * 50}px` : 'display:none'
    })

    helpGroup.push(group)
  })

  const themeData = await Theme.getThemeData(diyCfg.helpCfg || {}, sysCfg.helpCfg || {})

  return await Common.render('help/index', {
    helpCfg: helpConfig,
    helpGroup,
    ...themeData,
    element: 'default'
  }, { e, scale: 1 })
}
